"""
AI Video Hub 使用示例
"""

import requests
import json
import time

# API基础URL
BASE_URL = "http://127.0.0.1:8000"

def create_project():
    """创建一个新项目"""
    print("📝 创建新项目...")
    
    project_data = {
        "title": "我的AI视频项目",
        "description": "使用AI生成视频内容的示例项目"
    }
    
    response = requests.post(f"{BASE_URL}/projects/", json=project_data)
    
    if response.status_code == 200:
        project = response.json()
        print(f"✅ 项目创建成功！ID: {project['id']}")
        return project['id']
    else:
        print(f"❌ 项目创建失败: {response.text}")
        return None

def generate_video_from_text(project_id):
    """从文本生成视频"""
    print("\n🎬 从文本生成视频...")
    
    video_request = {
        "prompt": "一只可爱的小猫在花园里玩耍，阳光明媚，花朵盛开",
        "project_id": project_id
    }
    
    response = requests.post(f"{BASE_URL}/generate-video-from-text/", json=video_request)
    
    if response.status_code == 200:
        result = response.json()
        print(f"✅ 视频生成请求已提交！状态: {result['status']}")
        print(f"   视频ID: {result['video_id']}")
        print(f"   输出路径: {result['output_path']}")
        return result['video_id']
    else:
        print(f"❌ 视频生成请求失败: {response.text}")
        return None

def get_all_projects():
    """获取所有项目"""
    print("\n📋 获取所有项目...")
    
    response = requests.get(f"{BASE_URL}/projects/")
    
    if response.status_code == 200:
        projects = response.json()
        print(f"✅ 找到 {len(projects)} 个项目:")
        for project in projects:
            print(f"   - {project['title']} (ID: {project['id']})")
        return projects
    else:
        print(f"❌ 获取项目失败: {response.text}")
        return []

def get_health_status():
    """检查API健康状态"""
    print("\n🏥 检查API健康状态...")
    
    response = requests.get(f"{BASE_URL}/health")
    
    if response.status_code == 200:
        health = response.json()
        print(f"✅ API状态: {health['status']}")
        print(f"   时间戳: {health['timestamp']}")
        return True
    else:
        print(f"❌ API健康检查失败: {response.text}")
        return False

def main():
    print("🎯 AI Video Hub 使用示例")
    print("="*50)
    
    # 检查API健康状态
    if not get_health_status():
        print("❌ API服务不可用，请确保服务已启动")
        return
    
    # 创建项目
    project_id = create_project()
    if not project_id:
        print("❌ 无法创建项目，退出示例")
        return
    
    # 获取所有项目
    response = requests.get(f"{BASE_URL}/projects/")
    if response.status_code == 200:
        projects = response.json()
        print(f"\n📋 找到 {len(projects)} 个项目:")
        for project in projects:
            print(f"   - {project['title']} (ID: {project['id']})")
    else:
        print(f"❌ 获取项目列表失败: {response.text}")
    
    # 从文本生成视频
    video_id = generate_video_from_text(project_id)
    if video_id:
        print(f"\n💡 视频生成任务已启动 (ID: {video_id})")
        print("   请注意：实际视频生成可能需要几分钟时间")
        print("   您可以通过视频处理服务监控进度")
    
    print("\n🎉 示例执行完成！")
    print(f"🌐 访问 http://127.0.0.1:8000/docs 查看完整的API文档")

if __name__ == "__main__":
    main()
